layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        $form = $('form');
    okLoading.close();
    //添加验证规则verify
    var user = new Object();

    $.ajax({
        cache: true,
        type: "POST",
        url:"selectOne?uid="+$("#adminId").val(),
        async: false,
        error: function(request) {
            alert("失败!");
        },
        success: function(info) {
            user = info;
        }
    });



    form.verify({
        pass: [
            /^[\S]{6,16}$/
            , '密码必须6到16位，且不能出现空格'
        ],
        oldPwd: function (value, item) {
            if (value != user.password) {
                return "密码错误，请重新输入！";
            }
        },
        confirmPwd: function (value, item) {
            if (!new RegExp($("#oldPwd").val()).test(value)) {
                return "两次输入密码不一致，请重新输入！";
            }else{
                user.password = $("#oldPwd").val();
            }

        },

    });

    //修改密码
    form.on("submit(changePwd)", function (data) {
        var index = layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.post("/userChangePasswordByUid",{"uid":user.uid,"password":user.password},function (data) {
            setTimeout(function () {
                layer.close(index);
                layer.msg("密码修改成功,2秒之后将跳转到登录页面！");
                $(".pwd").val('');
                var le = $("#userLevel").val();
                if(le == 'admin'){
                setTimeout(function () {
                    top.window.location.href="/login-admin";
                }, 2000);
                }else if(le == 'user'){
                    setTimeout(function () {
                        top.window.location.href="/login";
                    }, 2000);
                }else {
                    setTimeout(function () {
                        top.window.location.href="/login-coach";
                    }, 2000);
                }
            }, 2000);
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })

});

